package io.gbznt.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class PropertyUtils {

	private static Logger logger = LoggerFactory.getLogger(PropertyUtils.class);
	 
	public static String getPropertyValueByKey(String key) {
		Properties props = new Properties();
		try {
			
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("configure.properties"));
			return props.getProperty(key).trim();
		} catch (Exception e) {
			logger.error(e.toString(),e);
			return null;
		}

	}

	/**
	 * 测试代码，请勿使用在开发中
	 * @param key
	 * @param value
	 * @param path
	 */
	public static void setPropertyValueByKey(String key, String value, String path) {
		Properties props = new Properties();
		
		try {
			System.out.println("属性文件：" + PropertyUtils.class.getClassLoader().getResource("configure.properties").getPath());
			OutputStream fos = new FileOutputStream( PropertyUtils.class.getClassLoader().getResource("configure.properties").getPath());   
			props.setProperty(key, value);
			props.store(fos, "Update '" + key + "' value"); 
			fos.close();
		} catch (Exception e) {
			logger.error(e.toString(),e);
		}
	}
	
	public static String getPropertyValueByKey(String propertyName,String key) {

		Properties props = new Properties();
		try {
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(propertyName));
			return props.getProperty(key).trim();
		} catch (Exception e) {
			logger.error(e.toString(),e);
			return null;
		}

	}
	
	public static Properties getProperty(String propertyName) {
		Properties props = new Properties();
		try {
			props.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream(propertyName));
			
		} catch (Exception e) {
			logger.error(e.toString(),e);
		}
		return props;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/*List l = PropertyUtils.getText();
		System.out.println("pch--->"+l.get(0));
		System.out.println("zh--->"+l.get(1));
		System.out.println("qyzb--->"+l.get(2));
		System.out.println("qysl--->"+l.get(3)); 
		System.out.println("zfry--->"+l.get(4));
		System.out.println("a--->"+l.get(5));
		System.out.println("b--->"+l.get(6));
		System.out.println("c--->"+l.get(7));
		System.out.println("d--->"+l.get(8));
		System.out.println("比例："+l.get(9));*/
		//System.out.println(getPropertyValueByKey("filepath"));
		
		String imagPath = getPropertyValueByKey("imagepath");
		System.out.println("22" + imagPath);
		
		setPropertyValueByKey("imagepath","F:/apache-tomcat-7.0.63/images","");
		String imagPath2 = getPropertyValueByKey("imagepath");
		System.out.println(imagPath2);
		
	}
	


}
